package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.ExternalContactLetter;
import com.amsp.oa.oademo.service.ExternalContactLetterService;
import com.amsp.oa.oademo.vo.ExternalContactLetterVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 外部联络函Controller
 */
@Api(tags = "外部联络函管理")
@RestController
@RequestMapping("/api/external-contact-letter")
public class ExternalContactLetterController {

    @Autowired
    private ExternalContactLetterService externalContactLetterService;

    @ApiOperation("保存或更新外部联络函")
    @PostMapping("/save")
    public ResponseEntity<Map<String, Object>> saveOrUpdate(@RequestBody ExternalContactLetterVO vo) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = externalContactLetterService.saveOrUpdateExternalContactLetter(vo);
            if (success) {
                result.put("code", 200);
                result.put("message", "保存成功");
            } else {
                result.put("code", 500);
                result.put("message", "保存失败");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "保存失败：" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation("批量保存或更新外部联络函")
    @PostMapping("/batch-save")
    public ResponseEntity<Map<String, Object>> batchSaveOrUpdate(@RequestBody List<ExternalContactLetterVO> voList) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = externalContactLetterService.batchSaveOrUpdateExternalContactLetter(voList);
            if (success) {
                result.put("code", 200);
                result.put("message", "批量保存成功");
            } else {
                result.put("code", 500);
                result.put("message", "批量保存失败");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "批量保存失败：" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation("获取外部联络函详情")
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getDetail(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            ExternalContactLetterVO vo = externalContactLetterService.getExternalContactLetterDetail(id);
            if (vo != null) {
                result.put("code", 200);
                result.put("message", "获取成功");
                result.put("data", vo);
            } else {
                result.put("code", 404);
                result.put("message", "数据不存在");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取失败：" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation("分页查询外部联络函")
    @GetMapping("/page")
    public ResponseEntity<Map<String, Object>> getPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            ExternalContactLetterVO vo) {
        Map<String, Object> result = new HashMap<>();
        try {
            Page<ExternalContactLetter> page = new Page<>(current, size);
            Page<ExternalContactLetterVO> voPage = externalContactLetterService.getExternalContactLetterPage(page, vo);

            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", voPage);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation("删除外部联络函")
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> delete(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = externalContactLetterService.deleteExternalContactLetter(id);
            if (success) {
                result.put("code", 200);
                result.put("message", "删除成功");
            } else {
                result.put("code", 500);
                result.put("message", "删除失败");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "删除失败：" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation("批量删除外部联络函")
    @DeleteMapping("/batch")
    public ResponseEntity<Map<String, Object>> batchDelete(@RequestBody List<Long> ids) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = externalContactLetterService.batchDeleteExternalContactLetter(ids);
            if (success) {
                result.put("code", 200);
                result.put("message", "批量删除成功");
            } else {
                result.put("code", 500);
                result.put("message", "批量删除失败");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "批量删除失败：" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
}